﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Collections.Specialized.BitVector32;

namespace listT
{
    internal class Program
    {
        static void Main(string[] args)
        {
            GenericList<int> alist = new GenericList<int>();
            for(int i = 0; i <= 10; i++)
            {
                alist.Add(i);
            }
            Action<int> action1 = item =>Console.Write($"{item} ");
            alist.ForEach(action1);
            Console.WriteLine();
            int max = alist.Head.Data, min = alist.Head.Data, sum = 0;
            Action<int> action2 = item => { if (item > max) max = item; };
            alist.ForEach(action2);
            Console.WriteLine($"最大值为：{max}");
            Action<int> action3 = item => { if (item < min) min = item; };
            alist.ForEach(action3);
            Console.WriteLine($"最小值为：{min}");
            Action<int> action4 = item => sum += item;
            alist.ForEach(action4);
            Console.WriteLine($"总和为：{sum}");
            Console.Read();
        }
        
        public class Node<T>
        {
            public Node<T> Next { get; set; }
            public T Data { get; set; }
            public Node(T t)
            {
                Next = null;
                Data = t;
            }
        }
        public class GenericList<T>
        {
            private Node<T> head;
            private Node<T> tail;   
            public GenericList()
            {
                tail = head = null;

            }
            public Node<T> Head
            {
                get => head;
            }
            public void Add(T t)
            {
                Node<T> n = new Node<T>(t);
                if(tail == null)
                {
                    head = tail = n;
                }
                else
                {
                    tail.Next = n;
                    tail = n;
                }
                
            }
            public void ForEach(Action<T> action)
            {
                Node<T> node = head;
                while(node != null)
                {
                    action(node.Data);
                    node = node.Next;
                }
            }
        }
    }
}
